import React, {Component} from 'react';
import {BrowserRouter, Switch, Route, Redirect} from "react-router-dom";
import './index.css';

// 导入common组件
import Common from "./Common";

// 导入路由配置信息
import Routes from './routerConfig';

// import {CSSTransition, TransitionGroup} from "react-transition-group";

class Index extends Component {
	render() {
		return (
			<BrowserRouter>
				<Route children={(props) => {
					// console.log(props);
					// console.log(Routes);
					// let {history, location} = props;
					let {location} = props;
					return (
						/*<TransitionGroup>
							<CSSTransition timeout={0} classNames={history.action == 'POP' ? 'pop' : 'push'}
								key={location.pathname}>*/
						<Switch location={location}>
							{
								Routes.map((route, index) => {
									return this.createChildRoute(route, index);
								})
							}
						</Switch>
						/*</CSSTransition>
					</TransitionGroup>*/
					)
				}}>
				</Route>
			</BrowserRouter>
		);
	}

	// 定义函数完成普通路由的构建
	createBaseRoute(route, index) {
		// console.log(1);
		// console.log(route,index);

		return <Route exact path={route.path} render={
			(props) => {
				// console.log(2);
				// console.log('route_props', props);
				// console.log('route', route);
				return (
					<Common meta={route.meta} {...props}>
						<route.component {...props}/>
					</Common>
				)
			}
		} key={index}/>
	}

	// 定义函数完成子路由的构建
	createChildRoute(route, index) {
		// console.log(route);
		// 获取当前路由的相关配置信息
		let {path, component: Father, children} = route;

		if (children) {
			console.log('hasChild',route);
			return (
				<Route path={path} key={index} children={() => {
					console.log(path, children[0].path);
					return (
						<Father>
							<Switch>
								{
									children.map((route2, index2) => {
										return this.createChildRoute({...route2, path: path + '/' + route2.path}, `${index}-${index2}`);
									})
								}
								<Redirect from={path} to={path + '/' + children[0].path}/>
							</Switch>
						</Father>
					)
				}
				}/>
			)
		} else {
			// 生成普通路由实例
			return this.createBaseRoute(route, index);
		}
	}
}

export default Index;